package com.dingyx.ps.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.dingyx.ps.util.ThreadLocalUtil;
import com.dingyx.ps.model.vo.AccountVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author dingyuxin
 *
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        AccountVO accountVO = ThreadLocalUtil.getAccount();
        setFieldValByName("createBy", Objects.nonNull(accountVO) ? accountVO.getUserId() : 0, metaObject);
        setFieldValByName("createDate", LocalDateTime.now(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        AccountVO accountVO = ThreadLocalUtil.getAccount();
        setFieldValByName("updateBy", Objects.nonNull(accountVO) ? accountVO.getUserId() : 0, metaObject);
        setFieldValByName("updateDate", LocalDateTime.now(), metaObject);
    }
}